<?php
/**
 * 采集文章Redis模型
 * @App	
 * @Group 
 * @Action SpiderArticleRedis
 * @author lilei
 * @date 2018年10月26日
 *
 */
namespace app\common\redis;

class SpiderArticleRedis extends RedisModel {
	/**
	 * Redis配置名称
	 * @var string
	 */
	public $redis = 'spiderDownQueue';
	
	/**
	 * Redis key
	 * @var string
	 */
	public $key = '';
	
	/**
	 * Redis key 前缀
	 */
	public $key_prefix = 'spider_article';
	
	/**
	 * 下载队列Key
	 * @var string
	 */
	const SpiderDownLoadQueue = "spiderDownQueue";
	
	/**
	 * 向下载队列添加一条数据
	 * @param int|array $download_id 下载编号或者下载编号列表
	 */
	public function pushSpiderDownLoadQueue( $download_id ) {
		if ( !$download_id ) {
			return false;
		}
		$this->SetRedisKey(self::SpiderDownLoadQueue);
		if ( is_array($download_id) ) {
			$this->ListMutiLpush($download_id);
		}elseif ( is_numeric($download_id) ) {
			$this->ListLpush($download_id);
		}
		return true;
	}
	
	/**
	 * 从下载队列取出一条下载编号
	 */
	public function popSpiderDownLoadQueue() {
		$this->SetRedisKey(self::SpiderDownLoadQueue);
		$did = $this->ListRPop();
		if ( !$did ) {
			return false;
		}
		return $did;
	}
	
	/**
	 * 获取下载队列数量
	 */
	public function sizeSpiderDownLoadQueue() {
		$this->SetRedisKey(self::SpiderDownLoadQueue);
		$size = $this->ListSize();
		return (int)$size;
	}
	
	/**
	 * 清空下载队列
	 */
	public function clearSpiderDownLoadQueue() {
		$this->SetRedisKey(self::SpiderDownLoadQueue);
		$this->Delete();
	}
	
	/**
	 * 重建下载队列
	 */
	public function rebuildSpiderDownLoadQueue() {
		$this->SetRedisKey(self::SpiderDownLoadQueue);
		// 先删除
		$this->clearSpiderDownLoadQueue();
		// 再重建
		$modelSpiderDownLoad = new SpiderDownloadModel();
		$map = [
				'is_down' => -1, // 未完成
		];
		$downLoadList = $modelSpiderDownLoad->search_id_list_no_page($map);
		if ( $downLoadList ) {
			$this->ListMutiLpush($downLoadList);
		}
	}
	
	
	
	
	
	
}